﻿package game 
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import game.events.EventData;
	/**
	 * ...
	 * @author Nguyen Thi Bich Phuong (Daisy)
	 */
	public class ChipController extends MovieClip {
		
		public var chip0:Chip;
		public var activeChip:Chip;
		
		public var chipArr:Array;
		public var chipValueArr:Array;				
		public var chipValueArrText:Array;	
		
		public var chipInitPosition:Number = 0;
		public var chip_controller_pos:int = 0;
		
		public function ChipController() {
			this.chipArr = new Array();			
			this.activeChip = this.chip0;		
			
			this.chipInitPosition = this.x;
			
			this.chipValueArr = new Array(0.1, 0.2, 0.5, 1, 2, 5,
										1, 2, 5, 10, 20, 50,
										10, 20, 50, 100, 200, 500,			
										100, 200, 500, 1000, 2000, 5000,
										1000, 2000, 5000, 10000, 20000, 50000,
										10000, 20000, 50000, 100000, 200000, 500000,
										100000, 200000, 500000, 1000000, 2000000, 5000000);
										
			this.chipValueArrText = new Array("0.1", "0.2", "0.5", "1", "2", "5",
											"1", "2", "5", "10", "20", "50",
											"10", "20", "50", "100", "200", "500",
											"100", "200", "500", "1000", "2000", "5000",
											"1000", "2000", "5000", "10K", "20K", "50K",
											"10K", "20K", "50K", "100K", "200K", "500K",
											"100K", "200K", "500K", "1M", "2M", "5M");
			//initChipsValue();
		}	
		
		public function initChipsValue(value:int):void {
			var xPos:int = chip0.x;
			var dis:int = value;
			for (var i = 0; i < this.chipValueArrText.length; i++) {
				var chip:Chip;
				if (i > 0) {
					chip = new Chip();
					this.addChild(chip);
					chip.transform = this.chip0.transform;
					chip.x = xPos;
				}else {
					chip = this.chip0;					
				}				
				chip.chipName = "Chip" + i;
				chip.setFrameIndex(i%6 * 3 + 1);
				chip.valueTf.text = this.chipValueArrText[i] + "";				
				chip.value = Number(this.chipValueArr[i]);
				chip.addEventListener(MouseEvent.MOUSE_DOWN, chipMouseDownHdl);
				this.chipArr.push(chip);
				xPos += dis;
			}		
			this.activeChip = this.chip0;
		}
		
		public function chipMouseDownHdl(e:MouseEvent):void {
			/*
			if (e.currentTarget != this.activeChip) {
				this.activeChip.unActive();
				this.activeChip = Chip(e.currentTarget);
			}
			var event:KenoEvent = new KenoEvent(KenoEvent.CHIP_CLICK, true);
			dispatchEvent(event);
			*/			
			trace("click chip");
			if (e.currentTarget != this.activeChip) {
				this.activeChip.unActive();
				this.activeChip = Chip(e.currentTarget);		
				
				var event:EventData = new EventData(EventData.CHIP_ACTIVE, true);
				event.chipActive = this.activeChip.chipName;				
				dispatchEvent(event);
			}
		}		
		
		/**
		 * @author Daisy
		 * @param	chipValueArr
		 */
		public function initChips(chipValueArr:Array):void {
			this.chipValueArr = chipValueArr;
			var xPos:int = chip0.x;
			var dis:int = 62;
			for (var i:int = 0; i < this.chipValueArr.length; i++) {
				var chip:Chip;
				if (i > 0) {
					chip = new Chip();
					this.addChild(chip);
					chip.transform = this.chip0.transform;
					chip.x = xPos;
				}else {
					chip = this.chip0;
				}
				chip.chipName = "Chip" + i;
				chip.setFrameIndex(i * 3 + 1);
				chip.valueTf.text = this.chipValueArr[i];
				chip.value = Number(this.chipValueArr[i]);
				chip.addEventListener(MouseEvent.MOUSE_DOWN, chipMouseDownHandler);
				this.chipArr.push(chip);
				xPos += dis;
			}
			this.chip0.setActive();
		}
		
		/*public function bac3InitChips(chipValueArr:Array):void {
		    this.chipValueArr = chipValueArr;
		    var xPos:int = chip0.x;
		    var dis:int = 62;
		   
		    for (var i:int = 0; i < this.chipValueArr.length; i++) {
				var chip:Chip;
				if (i > 0) {
					 chip = new Chip();
					 this.addChild(chip);
					 chip.transform = this.chip0.transform;
					 //chip.x = xPos;
					 //Code position chip 
					 if (i == 1) {
						//trace("Position: " + chip.y);
						chip.x = 130;      
					} else if (i == 2) {
						  chip.x = 76;
						  chip.y = 58;
					} else if (i == 3) {
						  chip.x = chip0.x;
						  chip.y = 95;
					} else if (i == 4) {
						  chip.x = 130;
						  chip.y = 95;
					}else {
							chip.x = xPos;
					}
				}else {
					chip = this.chip0;
				}
				chip.chipName = "Chip" + i;
				chip.setFrameIndex(i * 3 + 1);
				chip.valueTf.text = this.chipValueArr[i];
				chip.value = Number(this.chipValueArr[i]);
				chip.addEventListener(MouseEvent.MOUSE_DOWN, chipMouseDownHandler);
				this.chipArr.push(chip);
				xPos += dis;
			}
		   this.chip0.setActive();
		}*/
		
		public function bac3InitChips(maxBet:Number):void {					
			setChipPos(this.getChipPostion(maxBet));			
		}
		/**
		 * 
		 * @param	chipPos
		 * @usage	the function set position for chip of 3 baccarat
		 */
		public function setChipPos(chipPos:int) {
			trace("daisy set chipPos: " + chipPos);
			this.chip_controller_pos = chipPos;
			this.x = this.chipInitPosition;
			this.x = this.x - (42 * 3 * chipPos);
			this.activeChip.unActive();
			this.activeChip = Chip(this.chipArr[chipPos * 6]);
			Chip(this.chipArr[chipPos * 6]).setActive();			
		}
		/**
		 * @author	Bamboo
		 * @param	chipPos
		 * @param	value
		 * @usage	The function set position for chip in the game as Fantang
		 */
		public function resetChipPos(chipPos:int, value:int) {
			this.chip_controller_pos = chipPos;
			this.x = this.chipInitPosition;
			this.x = this.x - (value * 6 * chipPos);
			this.activeChip.unActive();
			this.activeChip = Chip(this.chipArr[chipPos * 6]);
			Chip(this.chipArr[chipPos * 6]).setActive();			
		}
		
		/**
		 * @author Daisy
		 * @param	e
		 */
		private function chipMouseDownHandler(e:MouseEvent):void {
			if (e.currentTarget != this.activeChip) {
				this.activeChip.unActive();
				this.activeChip = Chip(e.currentTarget);		
				
				var event:EventData = new EventData(EventData.CHIP_ACTIVE, true);
				event.chipActive = this.activeChip.chipName;				
				dispatchEvent(event);
			}
		}
		
		public function getChipPostion(maxBet:Number):Number {
			var arrChipMax:Array = new Array(5, 50, 500, 5000, 50000, 500000, 5000000);
			for (var i = 0; i < arrChipMax.length; i++) {
				if (maxBet < Number(arrChipMax[i])) {
					return (i-1);					
				}
			}
			return 6;
		}
		
	}
}